import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePayResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

import java.util.UUID;

public class Main {

    public static void main(String[] args) throws Exception {

        // 1. 设置参数（全局只需设置一次）

        Factory.setOptions(getOptions());

        try {

            // 2. 发起API调用（以创建当面付收款二维码为例）

            AlipayTradePagePayResponse response = Factory.Payment.Page().pay(
                    "支付成功", UUID.randomUUID().toString(), "100", "http://127.0.0.1:6002/pay.success.html");


            // 3. 处理响应或异常

            if (ResponseChecker.success(response)) {

                System.out.println("调用成功");

            } else {

                System.err.println("调用失败，原因：" + response + "，" + response);

            }

        } catch (Exception e) {

            System.err.println("调用遭遇异常，原因：" + e.getMessage());

            throw new RuntimeException(e.getMessage(), e);

        }

    }

    private static Config getOptions() {

        Config config = new Config();

        config.protocol = "https";

        config.gatewayHost = "openapi.alipaydev.com";

        config.signType = "RSA2";

        config.appId = "2021000121632657";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中

        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDC0U+6rEwvxq2RPbnepaV2rrnx2T+8HMb0FtId+uyh34Z5Vz6Hkm04ir7eNzbysvdKDOqwCxqplFy2VDfUWE4H/0C3CUmKOCiiR3vwdzyWWAbap7x9JqsrFJbOwqwGbBh4q3sbHQyTO56RXJp6NMZMjmY+OHlqcD/go5KEx3glQzbxC9YAH3YRHir9Gfs/he8jUXnF9AJhe8g1yZDKLRItYJpAVhgLLBu9dWv2Qsl4Cr71Q7wsAHNAiVh9IRfS4D3iTKWd2iesv6yG6lba2URcT+YL1Prfqj6vxRv55SIYzud23CVluL2dYcBiGuRk1tb9Insmeuko/vXPIfp+FVs3AgMBAAECggEABJ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FXVwNe+fwXcPQqIbnybMNI5lurmUBNUtaRb9GhGnrC+jr1m71+KVn5vpOpYCVCEahWoFaW07KZMqLgW3+MKXb+9ve5JRio9TIe4LfBW2tNioWX7ybMcP0RGqzdo7IEQ9JKqw6BAll252ZIzcvEB";

        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载

//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可

        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhL9hGtjeoAK9xm3AOnGLvEKlxnP6/sl8c5MxZZFvYbt7znEtFNljCBFtaOZnJtsKn7sphMDONpTMr0lN6aKT3vga6g6EM/HP1wRWywUOzWJqLbNNEJyKHL+5rfOn+d0e24tVuDI9dEw80BAVUc9NSV7coB0QMlZ+xDoKQ0iDc5dElLmB/TAmZ2bSpC8eXBiziUvexLZLyoheNPtJrmk/4ng2kHIziPOuMRfUrCZkEyh+LJ6EkhbnAikOgDMH4xWEUDtaxDyTTqhvRAyer/ARvi+F0hdi40/g12H60NO/Mle0iFQOCtMrkv+pa37hACfzv/K76rjZ4uvM33uHngXTnQIDAQAB";

        //可设置异步通知接收服务地址（可选）

        config.notifyUrl = "http://127.0.0.1:6002/pay.success.html";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）

        //config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return config;

    }

}